package com.example.phpclient.content;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.Group;
import com.example.phpclient.Manager;
import com.example.phpclient.R;

import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class manager_list extends AppCompatActivity {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private SQLiteDatabase dbWiter;
    private NotesDB notesDB;
    RequestQueue queue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notelist_activity);

        queue = Volley.newRequestQueue(getApplicationContext());
        notesDB = new NotesDB(this);

        jsonRequest("find_manager","123");
        preferences = getSharedPreferences("data",MODE_PRIVATE);
    }


    //jsonrequest请求数据
    public void jsonRequest(String aim,final String M_no){//通过组长编号查询教室
        String url =this.getString(R.string.recUrl);

        HashMap<String, String> params = new HashMap<String ,String>();
        params.put("forwhat",aim);
        params.put("M_no",M_no);
        JSONObject jsonObject =new JSONObject(params);
        Log.d("jsonObject",jsonObject.toString());
        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Group group1 = JSON.parseObject(jsonObject.toString(), Group.class);//解析json
                        dbWiter = notesDB.getWritableDatabase();
                        int i;
                        for(i = 0 ; i < group1.getMan_areas().size(); i++) {
                            ContentValues cv = new ContentValues();
                            cv.put(NotesDB.CONTENT,"");
                            cv.put(NotesDB.TIME, "");
                            cv.put(NotesDB.PATH, "");//图片插入数据库的是路径
                            cv.put(NotesDB.VIDEO,"");
                            cv.put(NotesDB.MAN_CLASS, group1.getMan_areas().get(i).getC_no());
                            cv.put(NotesDB.MAN_NO,group1.getMan_areas().get(i).getMan_no());

                            dbWiter.insert(NotesDB.TABLE_NAME, null, cv);
                        }
//                        Log.d("parase",group1.getMan_areas().size()+"");
//                        Log.d("parase",group1.getMan_areas().get(0).getMan_no());
//                        Log.d("parase",group1.getMan_areas().get(0).getC_no());
//                        Log.d("parase",group1.getMan_areas().get(1).getC_no());
//                        Log.d("parase",group1.getMan_areas().get(2).getC_no());
//
//                            editor = getSharedPreferences("data", MODE_PRIVATE).edit();
//                            editor.putString("man_no", manager.getMan_no());
//                            editor.putString("c_no", manager.getC_no());
//                            editor.apply();
//                            TextView textView = findViewById(R.id.note_class);
//                            textView.setText(manager.getC_no() + " !");


                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","Error");
                    }

                 });
        queue.add(jsonObjectRequest);
    }
}
